package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.RegBaseActivity;
import com.meihua.baoleyi.utils.MD5;
import com.meihua.baoleyi.utils.UrlContants;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Author:liming
 * Time: 2017/10/17
 * 找回支付密码
 */

public class FindPayPwdActivity extends RegBaseActivity {
    @Bind(R.id.text_back)
    ImageView textBack;
    @Bind(R.id.text_title)
    TextView textTitle;
    private Toast toast;
    @Bind(R.id.show_old_pwd)
    EditText showOldPwd;
    @Bind(R.id.codede)
    EditText codede;
    @Bind(R.id.show_zhanghao)
    TextView tvShowZhaohao;
    private String phone;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_reset_pay_pwd;
    }

    @Override
    public void onInitView() {
        ButterKnife.bind(this);
        textTitle.setText("验证旧密码");
        phone = myApp.getPhone().toString();
        phone = phone.replace(phone.substring(3, 7), "****");
        tvShowZhaohao.setText(phone);
    }

    @OnClick({R.id.text_back, R.id.reset_paypwd_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_back:
                finish();
                break;
            case R.id.reset_paypwd_submit:
                verifyOldPwd();
                break;
        }
    }
    //验证支付密码接口
    private void verifyOldPwd() {
        if (TextUtils.isEmpty(showOldPwd.getText()) || TextUtils.isEmpty(showOldPwd.getText().toString().trim())) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
        String str=String.valueOf(time);
        String token= myApp.getToken().toString();
        String sign_none="pay_password="+showOldPwd.getText().toString().toString()+"&timestamp="+str+"&token="+token+"&user_id="+myApp.getUser_id();
        String sign_yes = MD5.getMessageDigest(sign_none.toString().getBytes()).toUpperCase();

        OkGo.post(UrlContants.HOST+"api/account/verifyPayPassword")
                .tag(this)
                .params("pay_password",showOldPwd.getText().toString().toString())   //旧支付密码
                .params("timestamp",str)//时间戳
                .params("token",token)//token
                .params("user_id",myApp.getUser_id())  //用户id
                .params("sign",sign_yes)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getString("code").equals("200")) {
                                Toast.makeText(FindPayPwdActivity.this, json.getString("msg"), Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(FindPayPwdActivity.this, PayPwdActiity.class));
                                finish();
                            } else {
                                Toast.makeText(FindPayPwdActivity.this, "失败，" + json.getString("msg"), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

}
